<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange">
              <template slot-scope="scope" slot="menu">
                <el-button type="text"
                           size="small"
                           @click="goDetail(scope.row)">详情</el-button>
                <el-button type="text"
                           size="small"
                           @click="goEdit(scope.row)">编辑</el-button>
              </template>
              <template slot="menuLeft">
                <el-button type="primary" icon="el-icon-plus"  size="small" @click="addVisit">新增</el-button>
              </template>
            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
    import { fetchList } from '@/api/crm/customervisit';
    import { tableOption } from '../../../const/crud/crm/customervisit';
    import { mapGetters } from 'vuex';

    export default {
        name: 'customervisit',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.crm_customervisit_add, false),
                    delBtn: this.vaildData(this.permissions.crm_customervisit_del, false),
                    editBtn: this.vaildData(this.permissions.crm_customervisit_edit, false)
                };
            }
        },
        methods: {
            getList(page, params = {}) {
                this.tableLoading = true;
                // 拜访日期 时间段
                if (params.visitDate && params.visitDate.length === 2) {
                    params.startDate = params.visitDate[0];
                    params.endDate = params.visitDate[1];
                }
                delete params.visitDate;
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
                this.getList(this.page, this.filterForm(info.searchForm));
            },
            addVisit() {
              this.$router.push({path: '/customer/addVisit'})
            },
            goDetail(row){
              let query = {res:JSON.stringify(row)};
              this.$router.push({path: '/customer/detailVisit', query:query})
            },
            goEdit(row){
              let query = {res:JSON.stringify(row)};
              this.$router.push({path: '/customer/editVisit', query:query})
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
